package com.readbook.action;

import com.readbook.service.StudentService;
import com.readbook.util.ExcelUtils;
import com.readbook.util.OIDCardUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@EnableAutoConfiguration
@RequestMapping("/student")
public class student {
    @Resource
    private StudentService studentService;



    @RequestMapping("Search_Student.html")
    @ResponseBody
    public Map<String,Object>Search_Student(HttpServletRequest request, HttpServletResponse response){
        String sname=request.getParameter("sname").trim();
        String sfzh=request.getParameter("sfzh").trim();
        String nj=request.getParameter("nj").trim();
        String bj=request.getParameter("bj").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sname",sname);
        gmap.put("sfzh",sfzh);
        gmap.put("nj",nj);
        gmap.put("bj",bj);
        List<Map<String,Object>>list1=studentService.Search_Student(gmap);
        Map<String,Object>rmap=new HashMap<String,Object>();
        rmap.put("data",list1);
        return rmap;
    }


    @RequestMapping("del_Student.html")
    @ResponseBody
    public Map<String,Object> del_Student(HttpServletRequest request, HttpServletResponse response){
        String sid=request.getParameter("sid").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sid",sid);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("jlscsj", df.format(new Date()));
        Map<String,Object>getm=studentService.del_Student(gmap);
        return getm;
    }


    @RequestMapping("Insert_Student.html")
    @ResponseBody
    public Map<String,Object> Insert_Student(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object>smap=new HashMap<String,Object>();
        String sname=request.getParameter("sname").trim();
        String sfzh=request.getParameter("sfzh").trim();
        if(sfzh.equals("")){
            smap.put("result",false);
            smap.put("msg","请填写身份证！");
            return smap;
        }
        boolean va= OIDCardUtil.validateCard(sfzh);
        if(!va){
            smap.put("result",false);
            smap.put("msg","请填写正确的身份证！");
            return smap;
        }
        String nj=request.getParameter("nj").trim();
        String bj=request.getParameter("bj").trim();
        String jzsj=request.getParameter("jzsj").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sname",sname);
        gmap.put("sfzh",sfzh);
        gmap.put("nj",nj);
        gmap.put("bj",bj);
        gmap.put("jzsj",jzsj);
        gmap.put("sstate","0");
        String sex=OIDCardUtil.getGenderByIdCard(sfzh);
        gmap.put("sex",sex);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("jlxzsj", df.format(new Date()));
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("sid",uuid);
        int i=studentService.Insert_Student(gmap);
        if(i==3){
            smap.put("result",false);
            smap.put("msg","该学生已经存在！");
            return smap;
        }else if(i==1){
            smap.put("result",true);
            smap.put("msg","添加成功！");
            return smap;
        }else{
            smap.put("result",false);
            smap.put("msg","添加失败！");
        }
        return smap;
    }




    @RequestMapping("Edit_Student.html")
    @ResponseBody
    public Boolean Edit_Student(HttpServletRequest request, HttpServletResponse response){
        String sid="";
        if(request.getParameter("sid")!=null){
            sid=request.getParameter("sid").trim();
        }
        String sname="";
        if(request.getParameter("sname")!=null){
            sname=request.getParameter("sname").trim();
        }
        String sfzh="";
        if(request.getParameter("sfzh")!=null){
            sfzh=request.getParameter("sfzh").trim();
        }
        String nj="";
        if(request.getParameter("nj")!=null){
            nj=request.getParameter("nj").trim();
        }
        String bj="";
        if(request.getParameter("bj")!=null){
            bj=request.getParameter("bj").trim();
        }
        String jzsj="";
        if(request.getParameter("jzsj")!=null){
            jzsj=request.getParameter("jzsj").trim();
        }

        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sid",sid);
        gmap.put("sname",sname);
        gmap.put("sfzh",sfzh);
        gmap.put("nj",nj);
        gmap.put("bj",bj);
        gmap.put("jzsj",jzsj);
        int i=studentService.Edit_Student(gmap);
        if(i>=0){
            return true;
        }
        return false;
    }


    @RequestMapping("import_Student.html")
    @ResponseBody
    public Boolean import_Student(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Map<String,Object>>lsitdata=getExceldatas(request);
        int x=0;
        for(int i=0;i<lsitdata.size();i++){
            Map<String,Object>maps=lsitdata.get(i);
            String sname="";
            if(maps.get("姓名")!=null){
                sname=maps.get("姓名").toString().trim();
            }else{
                continue;
            }
            String sfzh="";
            if(maps.get("身份证号")!=null){
                sfzh=maps.get("身份证号").toString().trim();
                boolean va= OIDCardUtil.validateCard(sfzh);
                if(!va){
                    continue;
                }
            }else{
                continue;
            }
            String nj="";
            if(maps.get("年级")!=null){
                nj=maps.get("年级").toString().trim();
            }else{
                continue;
            }

            String bj="";
            if(maps.get("班级")!=null){
                bj=maps.get("班级").toString().trim();
            }else{
                continue;
            }

            String jzsj="";
            if(maps.get("家长手机号")!=null){
                jzsj=maps.get("家长手机号").toString().trim();
            }else{
                continue;
            }


            //判断身份证是否正确
            Map<String,Object>gmap=new HashMap<>();
            gmap.put("nj",nj);
            gmap.put("bj",bj);
            gmap.put("sname",sname);
            gmap.put("sfzh",sfzh);
            String sex=OIDCardUtil.getGenderByIdCard(sfzh);
            gmap.put("sex",sex);
            gmap.put("sstate","0");
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            gmap.put("sid",uuid);
            gmap.put("jzsj",jzsj);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            gmap.put("jlxzsj", df.format(new Date()));
            if(!sfzh.equals("")){
                int m=studentService.Insert_Student(gmap);
                if(m!=3 && m>0){
                    x++;
                }
            }

        }
        if(x>0){
            return true;
        }else{
            return false;
        }
    }




    //获取数据
    public List<Map<String,Object>>getExceldatas(HttpServletRequest request) throws IOException {
        List<Map<String,Object>>listdata=new ArrayList<>();
        String filename=request.getParameter("filename").toString();
        String filepath=request.getParameter("filepath").toString();
        String realpath=request.getRealPath(filepath);

        //后去文件路径与文件后缀
        String suffix = filename.substring(filename.lastIndexOf(".") + 1);
        //获取文件流
        File srcFile = new File(realpath);
        //创建文件字节输入流对象，并接在源上
        InputStream is = new FileInputStream(srcFile);
        //获取表头
        String row_head="";
        String[] columns_arr =null;

        if (suffix.equals("xls")) {
            HSSFWorkbook wbs = new HSSFWorkbook(is);
            //获取第一个单元格
            HSSFSheet childSheet = wbs.getSheetAt(0);
            //获取第一行 第一行为表头
            HSSFRow rowone = childSheet.getRow(0);
            if (null != rowone) {
                for (int k = 0; k < rowone.getLastCellNum(); k++) {
                    HSSFCell cell = rowone.getCell(k);
                    if (null != cell) {
                        if (k > 0) {
                            row_head += ",";
                            row_head += ExcelUtils.getHSSFCellValue(cell);
                        } else {
                            row_head += ExcelUtils.getHSSFCellValue(cell);
                        }
                    }
                }
            }

            if(row_head.trim().equals("")){
                return listdata;
            }
            columns_arr = row_head.split(",");
            //数据从第二行开始
            for(int j=1;j<=childSheet.getLastRowNum();j++){
                HSSFRow row=childSheet.getRow(j);
                StringBuilder row_value=new StringBuilder();
                if(null != row){
                    Map<String, Object>data_row=new HashMap<String,Object>();
                    for(int k = 0;k<columns_arr.length;k++){
                        HSSFCell cell = row.getCell(k);
                        if(cell != null){
                            int celltype=cell.getCellType();
                            String keys=columns_arr[k];
                            String values="";
                            if(celltype==3){
                                continue;
                            }else{
                                cell.setCellType(cell.CELL_TYPE_STRING);
                                values=ExcelUtils.getHSSFCellValue(cell);
                            }
                            data_row.put(keys,values);
                            row_value.append(values);
                        }
                    }
                    if(row_value.length() > 0){
                        listdata.add(data_row);
                        row_value.delete(0, row_value.length()-1);
                    }
                }
            }
        } else {
            XSSFWorkbook wbs = new XSSFWorkbook(is);
            //第一个工作表
            XSSFSheet childSheet = wbs.getSheetAt(0);
            //第一行
            XSSFRow rowone = childSheet.getRow(0);
            if (null != rowone) {
                for (int k = 0; k < rowone.getLastCellNum(); k++) {
                    XSSFCell cell = rowone.getCell(k);
                    if (null != cell) {
                        int celltype=cell.getCellType();
                        if(celltype==3){
                            continue;
                        }
                        if (k > 0)
                            row_head += ",";
                        row_head += ExcelUtils.getXSSFCellValue(cell);
                    } else {
                        row_head += ExcelUtils.getXSSFCellValue(cell);
                    }
                }
            }
            if(row_head.trim().equals("")){
                return listdata;
            }
            columns_arr = row_head.split(",");
            //数据从第二行开始
            for(int j=1;j<=childSheet.getLastRowNum();j++){
                XSSFRow row=childSheet.getRow(j);
                StringBuilder row_value=new StringBuilder();
                if(null != row){
                    Map<String, Object>data_row=new HashMap<String,Object>();
                    for(int k = 0;k<columns_arr.length;k++){
                        XSSFCell cell = row.getCell(k);
                        if(cell != null){
                            int celltype=cell.getCellType();
                            String keys=columns_arr[k];
                            String values="";
                            if(celltype==3){
                                continue;
                            }else{
                                cell.setCellType(cell.CELL_TYPE_STRING);
                                values=ExcelUtils.getXSSFCellValue(cell);
                            }
                            data_row.put(keys,values);
                            row_value.append(values);
                        }
                    }
                    if(row_value.length() > 0){
                        listdata.add(data_row);
                        row_value.delete(0, row_value.length()-1);
                    }
                }
            }
        }
        return listdata;
    }


}
